package nio.file;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class FilesTest1 {

	public static void main(String[] args) {
		
		Path dir = Path.of( "nio" );
		System.out.println( dir );
		
		if( Files.notExists( dir ) ) { // 判断 文件或目录 是否存在
			try {
				Path path = Files.createDirectory( dir ); // 创建目录
				System.out.println( path == dir ); // true
			} catch ( IOException e ){
				e.printStackTrace();
			}
		}
		
		Path file = Path.of( dir.toString() ,  "hello.txt" );
		
		if( Files.notExists( file ) ) { // 判断 文件或目录 是否存在
			try {
				Path path = Files.createFile( file ); // 创建文件
				System.out.println( path == file ); // true
			} catch ( IOException e ){
				e.printStackTrace();
			}
		}
		
		BufferedWriter writer = null ; 
		try {
			
			writer = Files.newBufferedWriter( file ); // 创建一个带有缓冲功能的字符输出流
			
			writer.append( "青海长云暗雪山" );
			writer.newLine();
			
			writer.write( "孤城遥望玉门关" );
			writer.newLine();
			
			writer.flush();
			
		} catch ( IOException e ){
			e.printStackTrace();
		} finally {
			if( writer != null ) {
				try {
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}

}
